package com.tbm.console.proj.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.domain.ResultCode;
import com.tbm.console.proj.entity.FrontWebConstantInfoEntity;
import com.tbm.console.proj.vo.FrontWebConstantInfoVO;
import com.tbm.console.util.DateUtil;
import com.tbm.console.util.LinkedHashMap2VO;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

//说明 字符串：FrontWebConstantInfo，作为替换字符串 LOWERFrontWebConstantInfo

@RestController
public class FrontWebConstantInfoJson extends BaseController{


    @Autowired
    private RestTemplate restTemplate;

    @Value("${restful.url}")
    private String url ;

    @Value("${restful.port}")
    private Integer port;

    @Autowired
    private HttpHeaders httpHeaders;


    /**
     * 显示list
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/frontWebConstantInfo-list/{page}/{size}")
    public Map<String,Object> frontWebConstantInfoList(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageData pageData = new PageData();
        JSONObject param2= new JSONObject();
        param2.put("page", page-1);
        param2.put("size",size);
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        httpHeaders.setContentType(mediaType);
        httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(JSON.toJSONString(param2), httpHeaders);
        ResultCode body = this.restTemplate.postForEntity(url + ":" + port + "/frontWebConstantInfo-get-paging", formEntity, ResultCode.class).getBody();
        List<PageData> list = new ArrayList<>();
        if(body.getCode()==1){
            list = LinkedHashMap2VO.getListPageData((List<LinkedHashMap>) body.getData());
        }
        pageData.put("total",1);
        pageData.put("rows", list);
        return pageData;
    }

    /**
     * 常量增删改
     * @return
     */
    @RequestMapping(value = "/frontWebConstantInfo-ins-upd-del")
    public Object logCreateUpdate(){
        PageData pd = this.getPageData();
        FrontWebConstantInfoVO frontWebConstantInfoVO = new FrontWebConstantInfoVO();
        //修改
        if(null!=pd.get("id")){
            //id存在表示修改
            frontWebConstantInfoVO.setId(Integer.valueOf(pd.get("id").toString()));
        }
        if(null!=pd.get("flag")){
            //flag存在表示删除
            frontWebConstantInfoVO.setDeleteFlag(InsUpdDelFlagConstant.DELETE_FLAG_Y);
        }
        if(null!=pd.get("key")){
            frontWebConstantInfoVO.setConstantKey(pd.getString("key"));
        }
        if(null!=pd.get("keyDesc")){
            frontWebConstantInfoVO.setConstantKeyDesc(pd.getString("keyDesc"));
        }
        if(null!=pd.get("value")){
            frontWebConstantInfoVO.setConstantValue(pd.getString("value"));
        }
        if(null!=pd.get("valueDesc")){
            frontWebConstantInfoVO.setConstantValueDesc(pd.getString("valueDesc"));
        }
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        httpHeaders.setContentType(mediaType);
        httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(JSON.toJSONString(frontWebConstantInfoVO), httpHeaders);
        ResultCode body = this.restTemplate.postForEntity(url + ":" + port + "/frontWebConstantInfo-save-update", formEntity, ResultCode.class).getBody();
        PageData map = new PageData();
        map.put("result", "success");
        return map;
    }   
}